package com.example.api.mall.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.AsyncTaskExecutor;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import javax.print.attribute.standard.Destination;

/**
 * 线程池配置
 * //@Ansync异步执行需要该配置
 */
@Configuration
public class AsyncConfig {

    private static final int MAX_POOL_SIZE = 50;
    private static final int CORE_POOL_SIZE = 20;
    private static final int QUEUE_CAPACITY = 5000;

    @Bean("asyncTaskExecutor")
    public AsyncTaskExecutor asyncTaskExecutor(){
        ThreadPoolTaskExecutor asyncTaskExecutor = new ThreadPoolTaskExecutor();
        asyncTaskExecutor.setMaxPoolSize(MAX_POOL_SIZE);  //最大线程数
        asyncTaskExecutor.setCorePoolSize(CORE_POOL_SIZE);  //核心线程数
        asyncTaskExecutor.setQueueCapacity(QUEUE_CAPACITY); //工作队列大小
        asyncTaskExecutor.setWaitForTasksToCompleteOnShutdown(true);//线程池关闭的时候等待所有任务都完成再继续销毁其他的Bean
//        asyncTaskExecutor.setAwaitTerminationSeconds();
        asyncTaskExecutor.setThreadNamePrefix("async-task-thread-pool-");
        asyncTaskExecutor.initialize();
        return asyncTaskExecutor;
    }


}
